package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Category;
import com.itheima.bean.Dish;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Tang
 * @date 2023/2/23
 */
@Slf4j
@RequestMapping("/dish")
@RestController
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> addDish(@RequestBody DishDto dishDto) {
        //1.调用service的新增方法
        dishService.addDishWithFlavors(dishDto);
        //2.返回结果
        return R.success("新增成功！");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> page(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pageSize, String name) {
        //0.添加分页拦截器
        //1.设置分页对象 Page对象
        Page<Dish> dishPage = new Page<>(page, pageSize);

        //2.设置查询条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, Dish::getName, name);

        //3.调用service的分页查询方法
        dishService.page(dishPage, wrapper);

        //优化：
        //1.创建一个新的page对象Page<DishDto>
        Page<DishDto> dishDtoPage = new Page<>();

        //2.直接将Page<Dish>对象所有属性复制给Page<DishDto>，可以排除掉records这个属性
        //BeanUtils.copyProperties(dishPage, dishDtoPage);
        //dishDtoPage获取里面的records
        /*List<DishDto> dishDtos;
        for (DishDto dishDto : dishDtos) {
            Category category = categoryService.getById(dishDto.getCategoryId());
            dishDto.setCategoryName();
        }*/

        BeanUtils.copyProperties(dishPage, dishDtoPage, "records");

        //3.设置Page<DishDto>的records属性，构造List<DishDto>数据
        List<DishDto> dishDtoList = new ArrayList<>();

        //3.1 循环遍历List<Dish>，
        List<Dish> dishList = dishPage.getRecords();
        for (Dish dish : dishList) {
            DishDto dishDto = new DishDto();
            //将dish数据复制给dishDto
            BeanUtils.copyProperties(dish, dishDto);  //dish: aa=11 bb==22,, dishDto: aa=11  cc=null
            //3.2 查询出category，将分类名称赋值给dishDto
            Category category = categoryService.getById(dish.getCategoryId());
            dishDto.setCategoryName(category.getName());

            //3.3 将dishDto添加到List<DishDto>
            dishDtoList.add(dishDto);
        }
        //4.设置Page<DishDto>的records属性
        dishDtoPage.setRecords(dishDtoList);

        //5.返回结果
        return R.success(dishDtoPage);

        //4.返回结果
        //return R.success(dishPage);
    }

    /**
     * 查询菜品信息包含口味信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> getDish(@PathVariable Long id) {
        //1.调用service的查询方法
        DishDto dishDto = dishService.getDishWithFlavors(id);
        //2.返回结果
        return R.success(dishDto);
    }

    /**
     * 修改菜品包含口味信息修改
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        //1.调用service的修改方法
        dishService.updateWithFlavors(dishDto);
        //2.返回结果
        return R.success("修改成功！");
    }

    /**
     * 查询菜品列表
     * @param dish
     * @return
     */
    /*@GetMapping("/list")
    public R<List<Dish>> list(Dish dish) {
        //1.创建查询对象
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();

        //2.设置查询条件
        wrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        wrapper.like(dish.getName() != null, Dish::getName, dish.getName());
        //只查询正在起售的菜品
        wrapper.eq(Dish::getStatus, 1);

        //3.调用service查询方法
        List<Dish> list = dishService.list(wrapper);

        //4.返回结果
        return R.success(list);
    }*/

    /**
     * 查询菜品列表包含口味信息
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish) {
        //1.调用service的查询方法
        List<DishDto> dishDtos = dishService.listWithFlavors(dish);
        //2.返回结果
        return R.success(dishDtos);
    }

}
